/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

/**
 *
 * @author BEHIEN
 */
public class Validation {
	
	public static String classify_command(String command)
	{
		 String[] command_arr;
		 command_arr = command.split(" ");
		
		return command_arr[0].trim();
	}
	
	public static String makeup_command(String command)
	{
		String new_command = "";
		
		new_command = command;
		
		new_command = new_command.replaceAll("\\n"," ");
		new_command = new_command.replaceAll("\\t"," ");
		
		new_command = new_command.toLowerCase();
		new_command = new_command.trim();
		
		
		String result = "";
		boolean flag = true;
		for(int i = 0; i < new_command.length(); i++)
		{
			char charAt = new_command.charAt(i);
			
			if(charAt == ' ' && flag == true)
			{
				result = result + Character.toString(charAt);
				flag = false;
				continue;
			}
			
			if(charAt == ' ' && flag == false)
				continue;
			
			result = result + Character.toString(charAt);
			flag = true;
		}
		
		return result;
	}
	
	
	
	
	// kiem tra gia tri: number, string
	public static boolean validate_value(String value)
	{
		try {
			
			Float.parseFloat(value);
        
        } catch (NumberFormatException ex) {
            
        	if(count_substring(value, "'") != 2 || value.charAt(0) != '\'' || value.charAt(value.length()-1) != '\'')
        		return false;
        }
		
		return true;
	}
	
	public static String clear_value(String value)
	{
		try {
			
			Float.parseFloat(value);
        
        } catch (NumberFormatException ex) {
            
        	if(count_substring(value, "'") == 2)
        	{
        		return value.replaceAll("'", "");
        	}
        }
		
		return value;
	}
	
	// kiem tra gia tri: number, string
	public static String get_type_value(String value)
	{
		try {
			
			Float.parseFloat(value);
        
        } catch (NumberFormatException ex) {
            
        	if(count_substring(value, "'") != 2 || value.charAt(0) != '\'' || value.charAt(value.length()-1) != '\'')
        		return "unknow";
        	
        	return "String";
        }
		
		return "int";
	}
	
	// dem so lan xuat hien cua 1 chuoi con
	public static int count_substring(final String string, final String substring)
	  {
	     int count = 0;
	     int idx = 0;

	     while ((idx = string.indexOf(substring, idx)) != -1)
	     {
	        idx++;
	        count++;
	     }

	     return count;
	  }
}
